package com.lxw.station;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxw.station.dto.GitProjectDTO;
import com.lxw.station.service.RestTemplateService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.http.HttpMethod;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class GitOps {
    public static void main(String[] args) throws GitAPIException {
        String remoteUrl = "https://github.com/Ravernclaw/The-latex-template-of-Jinan-University-undergraduate-thesis.git";
        String localPath = "E:\\csdn\\github\\test";
        clone(remoteUrl, localPath);

    }

    private static void extracted() {
        String curl = "curl 'https://api.indexea.com/v1/search/widget/wjawvtmm7r5t25ms1u3d?query=1048&q=%E6%AF%95%E4%B8%9A&from=0&size=20&sort_by_f=' \\\n" +
                "  -H 'Accept-Language: zh-CN,zh;q=0.9' \\\n" +
                "  -H 'Connection: keep-alive' \\\n" +
                "  -H 'Origin: https://so.gitee.com' \\\n" +
                "  -H 'Referer: https://so.gitee.com/?q=%E6%AF%95%E4%B8%9A' \\\n" +
                "  -H 'Sec-Fetch-Dest: empty' \\\n" +
                "  -H 'Sec-Fetch-Mode: cors' \\\n" +
                "  -H 'Sec-Fetch-Site: cross-site' \\\n" +
                "  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36' \\\n" +
                "  -H 'accept: application/json' \\\n" +
                "  -H 'sec-ch-ua: \"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"' \\\n" +
                "  -H 'sec-ch-ua-mobile: ?0' \\\n" +
                "  -H 'sec-ch-ua-platform: \"Windows\"' \\\n" +
                "  -H 'userid: df0be153-eab1-44be-9569-2bccaeff628b' \\\n" +
                "  -H 'x-token;' \\\n" +
                "  --compressed";
        String urlMb = "https://api.indexea.com/v1/search/widget/wjawvtmm7r5t25ms1u3d?query=1048&q=${q}from=${from}&size=100&sort_by_f=Star 数";

        int size = 100;
        int page = 1;
        int total;
        String q = "";
        int from;
        List<GitProjectDTO> projectDTOS = new ArrayList<>();
        do {
            from = (page - 1) * size;
            page++;
            String url = urlMb.replace("${q}", q).replace("${from}", String.valueOf(from));
            String exchange = RestTemplateService.exchange(url, HttpMethod.GET);
            total = JSON.parseObject(exchange).getJSONObject("hits").getJSONObject("total").getIntValue("value");
            JSONArray hits = JSON.parseObject(exchange).getJSONObject("hits").getJSONArray("hits");
            for (int i = 0; i < hits.size(); i++) {
                JSONObject fields = hits.getJSONObject(i).getJSONObject("fields");
                String id = fields.getJSONArray("id").toJavaList(String.class).get(0);
                String title = fields.getJSONArray("title").toJavaList(String.class).get(0);
                title = title.substring(title.indexOf("/") + 1);
                String description = fields.getJSONArray("description").toJavaList(String.class).get(0);
                String cloneUrl = fields.getJSONArray("url").toJavaList(String.class).get(0);
                projectDTOS.add(new GitProjectDTO(id, title, description, cloneUrl));
            }
        } while ((from + size) < total);
    }

    @SneakyThrows
    public static void clone(String remoteUrl, String localPath) {
        log.info(localPath);
        // 克隆远程Git仓库到本地
        File directory = new File(localPath);
        if (!directory.exists()) {
            if (directory.mkdirs()) {
                Git.cloneRepository()
                        .setURI(remoteUrl)
                        .setDirectory(directory)
                        .call();
                log.info("下载完成:{},路径:{}", remoteUrl,localPath);
            }
        }
    }
}
